﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    public class S_OtherCharPacks : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_OtherCharPacks ).FullName );

        private const int STATUS_POISON = 1;
        private const int STATUS_INVISIBLE = 2;
        private const int STATUS_PC = 4;
        private const int STATUS_FREEZE = 8;
        private const int STATUS_BRAVE = 16;
        private const int STATUS_ELFBRAVE = 32;
        private const int STATUS_FASTMOVABLE = 64;
        private const int STATUS_GHOST = 128;

        private byte[] _byte = null;

        public S_OtherCharPacks( L1PcInstance pc, bool isFindInvis )
        {
            buildPacket( pc, isFindInvis );
        }

        public S_OtherCharPacks( L1PcInstance pc )
        {
            buildPacket( pc, false );
        }

        private void buildPacket( L1PcInstance pc, bool isFindInvis )
        {
            int status = STATUS_PC;

            if ( pc.getPoison() != null )
            { // 毒状態
                if ( pc.getPoison().getEffectId() == 1 )
                {
                    status |= STATUS_POISON;
                }
            }
            if ( pc.isInvisble() && !isFindInvis )
            {
                status |= STATUS_INVISIBLE;
            }
            if ( pc.isBrave() )
            {
                status |= STATUS_BRAVE;
            }
            if ( pc.isElfBrave() )
            {
                // エルヴンワッフルの場合は、STATUS_BRAVEとSTATUS_ELFBRAVEを立てる。
                // STATUS_ELFBRAVEのみでは効果が無い？
                status |= STATUS_BRAVE;
                status |= STATUS_ELFBRAVE;
            }
            if ( pc.isFastMovable() )
            {
                status |= STATUS_FASTMOVABLE;
            }

            // int addbyte = 0;
            // int addbyte1 = 1;

            writeC( Opcodes.S_OPCODE_CHARPACK );
            writeH( pc.getX() );
            writeH( pc.getY() );
            writeD( pc.getId() );
            if ( pc.isDead() )
            {
                writeH( pc.getTempCharGfxAtDead() );
            }
            else
            {
                writeH( pc.getTempCharGfx() );
            }
            if ( pc.isDead() )
            {
                writeC( pc.getStatus() );
            }
            else
            {
                writeC( pc.getCurrentWeapon() );
            }
            writeC( pc.getHeading() );
            // writeC(0); // makes char invis (0x01), cannot move. spells display
            writeC( pc.getChaLightSize() );
            writeC( pc.getMoveSpeed() );
            writeD( 0x0000 ); // exp
            // writeC(0x00);
            writeH( pc.getLawful() );
            writeS( pc.getName() );
            writeS( pc.getTitle() );
            writeC( status );
            writeD( pc.getClanid() );
            writeS( pc.getClanname() ); // クラン名
            writeS( null ); // ペッホチング？
            writeC( 0 ); // ？
            /*
             * if(pc.is_isInParty()) // パーティー中 { writeC(100 * pc.get_currentHp() /
             * pc.get_maxHp()); } else { writeC(0xFF); }
             */

            writeC( 0xFF );
            writeC( 0 ); // タルクック距離(通り)
            writeC( 0 ); // PC = 0, Mon = Lv
            writeC( 0 ); // ？
            writeC( 0xFF );
            writeC( 0xFF );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = getBytes();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_OtherCharPacks";
        }
    }
}
